package com.gaber.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author gaber
 * @date 2023/8/1 14:31:12
 * @description TODO
 */
public class Java8Tester {
    public static void main(String[] args) {
        System.out.println("使用Java 7： ");

        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        System.out.println("列表：" + strings);
        int count = getCountEmptyStringUsingJava7(strings);
        System.out.println("空字符数量为: " + count);

        int count1 = getCountLength3UsingJava7(strings);
        System.out.println("字符串长度为 3 的数量为: " + count1);

        List<String> filtered  = deleteEmptyStringsUsingJava7(strings);
        System.out.println("筛选后的列表: " + filtered);

        // 删除空字符串，并使用逗号把它们合并起来
        String mergedString  = getMergedStringUsingJava7(strings, ",");
        System.out.println("合并字符串: " + mergedString);

        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取列表元素平方数
        List<Integer> squaresList = getSquares(numbers);
        System.out.println("平方数列表: " + squaresList);

        List<Integer> integers = Arrays.asList(1, 2, 13, 4, 15, 6, 17, 8, 19);
        System.out.println("列表：" + integers);
        System.out.println("列表中最大的数："+ getMax(integers));
        System.out.println("列表中最小的数 : " + getMin(integers));
        System.out.println("所有数之和 : " + getSum(integers));
        System.out.println("平均数 : " + getAverage(integers));
        System.out.println("随机数: ");
        // 输出10个随机数
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(random.nextInt());
        }

        System.out.println("使用 java8: ");
        System.out.println("列表：" + strings);
        long count2 = strings.stream().filter(string -> string.isEmpty()).count();
        System.out.println("空字符串数量为: " + count2);

        long count3 = strings.stream().filter(string -> string.length() == 3).count();
        System.out.println("字符串长度为 3 的数量为: " + count3);

        List<String> collect = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
        System.out.println("筛选后的列表: " + collect);

        String collect1 = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(","));
        System.out.println("合并字符串: " + collect1);

        List<Integer> collect2 = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println("Squares List: " + squaresList);
        System.out.println("列表: " +integers);

        IntSummaryStatistics stats = integers.stream().mapToInt(x -> x).summaryStatistics();
        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
        System.out.println("随机数: ");

        random.ints().limit(10).sorted().forEach(System.out::println);

        // 并行处理
        long count4 = strings.parallelStream().filter(s -> s.isEmpty()).count();
        System.out.println("空字符串的数量为: " + count4);
    }



    /**
     * 统计字符串为空的个数
     * */
    private static int getCountEmptyStringUsingJava7(List<String> strings) {
        int count = 0;
        for (String string: strings
             ) {
            if(string.isEmpty()) {
                count++;
            }
        }
        return count;

    }
    /**
     * 统计字符串长度为3个个数
     * */
    private static int getCountLength3UsingJava7(List<String> strings) {
        int count = 0;
        for (String string: strings
        ) {
            if(string.length() == 3) {
                count++;
            }
        }
        return count;
    }

    /**
     * 清空List中为空的数据
     * */
    private static List<String> deleteEmptyStringsUsingJava7(List<String> strings) {
        ArrayList<String> filteredList = new ArrayList<String>();

        for (String string :
                strings) {
            if(!string.isEmpty()) {
                filteredList.add(string);
            }
        }
        return filteredList;
    }
    /**
     * 清空List中为空的数据,并且转成字符串
     * */
    private static String getMergedStringUsingJava7(List<String> strings, String s) {
        StringBuilder stringBuilder = new StringBuilder();

        for (String string :
                strings) {
            if(!string.isEmpty()) {
                stringBuilder.append(string);
                stringBuilder.append(s);
            }
        }

        String mergedString = stringBuilder.toString();
        return mergedString.substring(0, mergedString.length()-2);
    }

    /**
     * 获取list值的平方
     * */
    private static List<Integer> getSquares(List<Integer> numbers) {
        List<Integer> squaresList  = new ArrayList<>();
        for (Integer number :
                numbers) {
            Integer square = new Integer(number.intValue() * number.intValue());

            // 查询是否已经存在该数值
            if(!squaresList.contains(square)){
                squaresList.add(square);
            }
        }
        return squaresList;
    }
    /**
     * 获取list的最大值
     * */
    private static int getMax(List<Integer> integers) {
        int max = integers.get(0);

        for (int i = 0; i < integers.size(); i++) {
            Integer number = integers.get(i);
            if(number.intValue() > max) {
                max = number.intValue();
            }
        }
        return max;
    }
    /**
     * 获取list的最小值
     * */
    private static int getMin(List<Integer> integers) {
        int min = integers.get(0);
        for (int i = 0; i < integers.size(); i++) {
            Integer number = integers.get(i);
            if(number.intValue() < min) {
                min = number.intValue();
            }
        }
        return min;
    }
    /**
     * 获取list的总和
     * */
    private static int getSum(List<Integer> integers) {
        int sum = 0;
        for (int i = 0; i < integers.size(); i++) {
            sum += integers.get(i);
        }
        return sum;
    }
    /**
     * 获取list的平均数
     * */
    private static int getAverage(List<Integer> integers) {
        int sum = 0;
        for (int i = 0; i < integers.size(); i++) {
            sum += integers.get(i);
        }
        return sum / integers.size();
    }

}
